<?php	namespace Admin\Controller;
/**
 * 商品管理控制器
 * @author 武斌  <wubin.mail@foxmail.com>
 * 判断管理员是否登陆,后台其他控制器均继承此控制器
 */
class GoodsController extends CommonController
{
	protected $db;
	protected $cate;
	protected $brand;
	protected $typeattr;
	protected $goodsAttr;
	protected $goodsDetails;
	public function __construct()
	{
		parent::__construct();
		$this->db = new \Admin\Model\Goods;//实例化商品模型
		$this->cate = new \Admin\Model\Cate;//实例化分类模型
		$this->brand = new \Admin\Model\Brand;//实例化品牌模型
		$this->typeattr = new \Admin\Model\TypeAttr;//实例化类型属性模型
		$this->goodsAttr = new \Admin\Model\GoodsAttr;//实例化商品属性模型
		$this->goodsDetails = new \Admin\Model\GoodsDetails;//实例化商品详情模型
	}
	/**
	 * 首页
	 */
	public function index()
	{	//调用商品模型中的getAll方法
		$res = $this->db->getAll();
		//分配变量，显示模板
		View::with('data',$res)->make();
	}
	/**
	 * 添加
	 */
	public function store()
	{	//用户点击确定添加按钮，有post提交之后执行
		if(IS_POST)
		{	//执行商品模型中的store方法
			if(!$this->db->store())
			{	//如果有错误，提示相应的错误信息
				$this->error($this->db->getError());
			}
			//添加成功，提示信息并跳转到首页
			View::success('商品添加成功',U('index'));
		}
		//分类数据，调用分类模型中的getAll方法
		$cateData = $this->cate->getAll();
		//品牌数据，调用品牌模型中的getAll方法
		$brandData = $this->brand->get();
		//分配变量，显示模板
		View::with('cateData',$cateData)->with('brandData',$brandData)->make('add');
	}
	/**
	 * 上传
	 */
	public function ajaxUpload()
	{
		$file = Upload::path('Upload/goods/' . date('y/m/d'))->make();
		if(!empty($file))
		{
			$data = $file[0];
			View::ajax($data);
		}
		View::ajax('上传失败');
	}
	/**
	 * 异步删除图片,将upload里面的图片一起删掉
	 */
	public function delImg()
	{
		$path = Q('post.path');
		if(unlink($path))
		{
			View::ajax(1);
		}
		View::ajax(0);
	}
	/**
	 * 异步调取规格属性
	 */
	public function getAttrSpec()
	{
		if(!IS_AJAX) $this->error('非法请求');
		$type_id = Q('ID');
		$typeAttrData = $this->typeattr->where('type_id',$type_id)->get();
		foreach ($typeAttrData as $k => $v) {
			$typeAttrData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		View::ajax($typeAttrData);
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!$this->db->edit())
			{
				$this->error($this->db->getError());
			}
			View::success('商品编辑成功',U('index'));
			
		}
		$gid=Q('get.gid',0,'intaval');
		$goodsData = $this->db->where('gid',$gid)->first();
		View::with('goodsData',$goodsData);
		//**********分类数据*************//
		$cid = $goodsData['category_cid'];
		$sonCids = $this->db->getSon($this->cate->get(),$cid);
		$data = implode(',', $sonCids);
		if(empty($data))
		{
			$data = $this->cate->get();
		}else
		{
			$data = $this->cate->where("cid not in($data)")->get();
		}
		$cateData = Data::tree($data,'cname');
		View::with('cateData',$cateData);
		//**********品牌数据*************//	
		$brandData = $this->brand->get();
		View::with('brandData',$brandData);
		//*********商品属性*************//
		$type_ID = $goodsData['type_ID'];
		View::with('type_ID',$type_ID);
		$typeAttr = $this->typeattr->where('type_ID',$type_ID)->get();
		foreach ($typeAttr as $k => $v) {
			$typeAttr[$k]['tavalue'] = explode(',',  $v['tavalue']);
		}
		View::with('typeAttr',$typeAttr);
		$typeAttrGavalue = $this->goodsAttr->where('goods_gid',$gid)->lists('gavalue');
		View::with('typeAttrGavalue',$typeAttrGavalue);
		//*********商品规格*************//
		$goodsAttrData = Db::table('typeattr ta')->join('goodsattr ga','ta.taid','=','ga.typeAttr_taid')->where('goods_gid',$gid)->get();
		foreach ($goodsAttrData as $k => $v) {
			$goodsAttrData[$k]['tavalue'] = explode(',',  $v['tavalue']);
		}
		View::with('goodsAttrData',$goodsAttrData);
		//********商品详情表************//
		$goodsDetail = $this->goodsDetails->where('goods_gid',$gid)->first();
		$goodsDetail['big'] = explode(',', $goodsDetail['big']);
		foreach ($goodsDetail['big'] as $k => $v) {
			$goodsDetail['big'][$k] = dirname($v) . '/' . ltrim(basename($v),'big');
		}
		View::with('goodsDetail',$goodsDetail);
		View::make();
	}

	/**
	 * 商品删除
	 */
	public function del()
	{
		$gid = Q('get.gid',0,'intaval');
		$this->db->where('gid',$gid)->delete();
		$this->goodsAttr->where('goods_gid',$gid)->delete();
		$this->goodsDetails->where('goods_gid',$gid)->delete();
		View::success('删除成功',U('index'));
	}
}
